import type { UploadFileResponse } from '@/app/api/upload/route';
import type { ApiResponse } from '@/libs/apiResponse';
import { clientFetch } from '@/utils/request';

export const uploadFile = async (
  file: File
): Promise<ApiResponse<UploadFileResponse>> => {
  const formData = new FormData();

  // 重命名文件名，避免中文或特殊字符
  const safeFileName = file.name.replace(/[^\w.\-]/g, '_');
  const renamedFile = new File([file], safeFileName, { type: file.type });

  formData.append('file', renamedFile);

  const res = await clientFetch.post('/api/upload', formData);

  return res;
};
